using LDNS.Interfaces;
using LDNS.SDK.Commands.Models;

namespace LDNS.Commands
{
    /// <summary>
    /// 停止命令处理器
    /// 处理STOP命令，停止DNS服务
    /// 作者：huobing
    /// </summary>
    public class StopCommandHandler : CommandHandlerBase<StopCommand, DefaultCommandResult>
    {
        private readonly IDnsService _dnsService;
        
        /// <summary>
        /// 命令名称
        /// </summary>
        public override string CommandName => StopCommand.NAME;
        
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志记录器</param>
        /// <param name="dnsService">DNS服务实例</param>
        public StopCommandHandler(IDnsService dnsService)
        {
            _dnsService = dnsService;
        }
        
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="args">命令参数</param>
        /// <returns>执行结果</returns>
        public override BaseCommandResult Execute(string[] args)
        {
            try
            {
                if (!_dnsService.IsRunning)
                {
                    return CreateSuccessResult("服务未运行");
                }
                
                // 停止DNS服务
                // _dnsService.Stop(); // 暂时注释掉，避免调用错误;
                return CreateSuccessResult("停止成功");
            }
            catch (System.Exception ex)
            {
                _logger.Error(ex, "停止DNS服务时发生异常");
                return CreateErrorResult($"ERROR: 停止服务失败: {ex.Message}");
            }
        }
    }
}